package com.zyg.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.zyg.common.utils.PageUtils;
import com.zyg.common.utils.Query;
import com.zyg.manager.dao.SpecificationOptionDao;
import com.zyg.manager.entity.SpecificationOptionEntity;
import com.zyg.manager.entity.group.Specification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zyg.manager.dao.SpecificationDao;
import com.zyg.manager.entity.SpecificationEntity;
import com.zyg.manager.service.SpecificationService;


@Service("specificationService")
public class SpecificationServiceImpl extends ServiceImpl<SpecificationDao, SpecificationEntity> implements SpecificationService {
    @Autowired
    private SpecificationOptionDao optionDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpecificationEntity> page = this.page(
                new Query<SpecificationEntity>().getPage(params),
                new QueryWrapper<SpecificationEntity>()
        );

        return new PageUtils(page);
    }

    //1. 添加规格
    @Override
    public boolean save(Specification specification) {
        //1.1 添加规格
        this.baseMapper.insert(specification.getSpec());    //添加后，specification.getSpec()的主键就有了值
        Long id = specification.getSpec().getId();
        System.out.println("id = " + id);

        //1.2 再添加规格选项
        for (SpecificationOptionEntity option : specification.getOptions()) {
            //1.2.1 关联添加的外键
            option.setSpecId(specification.getSpec().getId());
            //1.2.2 添加规格选项到数据库
            optionDao.insert(option);
        }

        return false;
    }
    //2. 修改规格选项（不管是一对多还是多对多关系，多的这方都是将其删除再添加）
    @Override
    public void update(Specification specification) {
        //2.1 修改规格
        this.baseMapper.updateById(specification.getSpec());
        //2.2 根据外键删除规格选项
        // List<SpecificationOptionEntity> options = specification.getOptions();
        QueryWrapper<SpecificationOptionEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("spec_id",specification.getSpec().getId());
        optionDao.delete(wrapper);
        //2.3 添加规格选项
        //2.3.1 再添加规格选项
        for (SpecificationOptionEntity option : specification.getOptions()) {
            //2.3.2 关联添加的外键
            option.setSpecId(specification.getSpec().getId());
            //2.3.3 添加规格选项到数据库
            optionDao.insert(option);
        }
    }

    @Override
    public Specification findById(Long id) {
        //0. 定义要查询到的组合对象
        Specification specification = new Specification();
        //1. 根据规格id查询 规格
        SpecificationEntity spec = this.baseMapper.selectById(id);
        //2. 查询规格选项对象
        List<SpecificationOptionEntity> options = optionDao.selectList(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id", id));
        //3. 添加组合对象
        specification.setSpec(spec);
        specification.setOptions(options);
        return specification;
    }

    @Override
    public void deleteIds(List<Long> ids) {
        //1. 根据规格id来删除规格及规格选项表
        baseMapper.deleteBatchIds(ids);
        //2. 再删除规格选项表
        optionDao.delete(new QueryWrapper<SpecificationOptionEntity>().in("spec_id",ids));

    }

}